# -*- coding: utf-8  -*-
#!/usr/local/bin/python

__author__ = 'jian.yao'

from django import template
from warehouse.backend import get_commodity_list
from misc.errorcode import ERROR_CODE, ERROR_MESSAGE
from ws.decorators import compile_response

def commodity_list_view(request):
    request.CUSTOM_VARY = True

    context = template.RequestContext(request)
    _channel = request.parameters.get("channel")
    if _channel is None or _channel == "":
        _channel = "littleOrange"
    _catagory = request.parameters.get("catagory")
    _output = request.parameters.get("output")

    _commodity_list = get_commodity_list(_catagory)
    _commodity_count = 0
    if _commodity_list:
        _commodity_count = _commodity_list.count()
    _result = True
    _errorcode = ERROR_CODE.SUCCESS
    _message = ERROR_CODE.SUCCESS

    extra_context = {
        "result" : _result,
        "errorcode"  : _errorcode,
        "message" : _message,
        "channel" : _channel,
        "output" : _output,
        "caragory" : _catagory,
        "commodity_count" : _commodity_count,
        "commodity_list" : _commodity_list,
    }

    context.update(extra_context)
    response = compile_response(request, "ws/commodity/show.xml", context)
    return response


